home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / FWSclBar.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.8 KB  |  474 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWSclBar.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWSCLBAR_H
  13. #include "FWSclBar.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWPART_H
  21. #include "FWPart.h"
  22. #endif
  23.  
  24. #ifndef FWSCLNOT_H
  25. #include "FWSclNot.h"
  26. #endif
  27.  
  28. #ifndef FWNOTDEF_H
  29. #include "FWNotDef.h"
  30. #endif
  31.  
  32. #ifndef FWCONTXT_H
  33. #include "FWContxt.h"
  34. #endif
  35.  
  36. #ifndef FWSCROLR_H
  37. #include "FWScrolr.h"
  38. #endif
  39.  
  40. // ----- OS Layer -----
  41.  
  42. #ifndef FWEVENT_H
  43. #include "FWEvent.h"
  44. #endif
  45.  
  46. #ifndef FWFONT_H
  47. #include "FWFont.h"
  48. #endif
  49.  
  50. #ifndef FWODGEOM_H
  51. #include "FWODGeom.h"
  52. #endif
  53.  
  54. // ----- OpenDoc Includes -----
  55.  
  56. #ifndef SOM_ODSession_xh
  57. #include <ODSessn.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODFacet_xh
  61. #include <Facet.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODTransform_xh
  65. #include <Trnsform.xh>
  66. #endif
  67.  
  68. // ----- Platform Includes -----
  69.  
  70. #if defined(FW_BUILD_MAC) && !defined(__CONTROLS__)
  71. #include <Controls.h>
  72. #endif
  73.  
  74. #ifdef FW_BUILD_WIN
  75. #include <Windows.h>
  76. #endif
  77.  
  78. //========================================================================================
  79. // File scope definitions
  80. //========================================================================================
  81.  
  82. #ifdef FW_BUILD_MAC
  83. #pragma segment fwgadgts
  84. #endif
  85.  
  86. //========================================================================================
  87. // CLASS FW_CScrollBar
  88. //========================================================================================
  89.  
  90. FW_DEFINE_AUTO(FW_CScrollBar)
  91. FW_DEFINE_CLASS_M1(FW_CScrollBar, FW_CNativeControl)
  92.  
  93. // This class is archivable, but we provide the archiving implementation in a separate
  94. // translation unit in order to enable deadstripping of the archiving-related code
  95. // in parts that do not use archiving with this class.
  96.  
  97. //----------------------------------------------------------------------------------------
  98. // FW_CScrollBar::FW_CScrollBar
  99. //----------------------------------------------------------------------------------------
  100.  
  101. FW_CScrollBar::FW_CScrollBar(Environment* ev, 
  102.                              FW_CSuperView* container, 
  103.                              const FW_CRect& bounds,
  104.                              ODID viewID) :
  105.     FW_CNativeControl(ev, container, bounds, viewID),
  106.     fMinorScrollUnits(FW_kFixedPos1),
  107.     fMajorScrollUnits(FW_kFixedPos1),
  108.     fScroller(NULL)
  109. {
  110.     Initialize(ev, GetValue(ev), FW_CScrollBar::kDefaultScrollMin, FW_CScrollBar::kDefaultScrollMax);
  111.  
  112.     // Bind the scroll-bar to right or bottom egde of its superview by default
  113.     FW_ViewBinding    sbBindings = (bounds.Height() > bounds.Width()) ? 
  114.                 FW_kFixedWidth + FW_kRightBinding + FW_kTopBinding + FW_kBottomBinding :
  115.                 FW_kFixedHeight + FW_kRightBinding + FW_kLeftBinding + FW_kBottomBinding;
  116.     SetBindings(ev, sbBindings);
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // FW_CScrollBar::FW_CScrollBar
  121. //----------------------------------------------------------------------------------------
  122.  
  123. FW_CScrollBar::FW_CScrollBar(Environment* ev) :
  124.     FW_CNativeControl(ev),
  125.     fMinorScrollUnits(FW_kFixedPos1),
  126.     fMajorScrollUnits(FW_kFixedPos1),
  127.     fScroller(NULL)
  128. {
  129.     FW_END_CONSTRUCTOR
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // FW_CScrollBar::~FW_CScrollBar
  134. //----------------------------------------------------------------------------------------
  135.  
  136. FW_CScrollBar::~FW_CScrollBar()
  137. {
  138.     FW_START_DESTRUCTOR
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // FW_CScrollBar::Initialize
  143. //----------------------------------------------------------------------------------------
  144.  
  145. void FW_CScrollBar::Initialize(Environment *ev, FW_ControlValue value, FW_ControlValue min,
  146.                             FW_ControlValue max)
  147. {
  148.     FW_CRect bounds = GetBounds(ev);
  149.     FW_ViewBinding sbBindings = GetBindings(ev);
  150.     
  151.     // Ensure that width of vert. SB and height or horiz. SB are constant
  152.     if (bounds.Height() > bounds.Width())
  153.     {
  154.         sbBindings |= FW_kFixedWidth;
  155.     } 
  156.     else
  157.     {
  158.         sbBindings |= FW_kFixedHeight;
  159.     }
  160.     SetBindings(ev, sbBindings);
  161.  
  162.     // Create the control helper
  163. #ifdef FW_BUILD_MAC
  164.     fControlHelper = new FW_CPrivMacScrollBarControlHelper(ev, this, value, min, max, scrollBarProc);
  165. #endif
  166.  
  167. #ifdef FW_BUILD_WIN
  168.     // TO UPDATE
  169.     fControlHelper  = new FW_CPrivWinControlHelper
  170.                                 (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinScrollBarHelper), this);
  171. #endif
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. // FW_CScrollBar::PrivSetValue
  176. //----------------------------------------------------------------------------------------
  177.  
  178. FW_Boolean FW_CScrollBar::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  179. {
  180.     // Update the native control.  Return false if value was not changed 
  181.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  182.     {
  183.         // Send a control message to its receivers
  184.         if (GetMessage(ev) != FW_kScrollMsg && GetMessage(ev) != FW_kNullMsg)
  185.             Notify(ev, FW_CControlNotification(this));
  186.  
  187.         return true;
  188.     }
  189.     else
  190.         return false;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // FW_CScrollBar::ControlClicked
  195. //----------------------------------------------------------------------------------------
  196.  
  197. void FW_CScrollBar::ControlClicked(Environment *ev, FW_ControlValue delta, ODFacet* facet)
  198. {    
  199.     // Update the control first
  200.     if (PrivSetValue(ev, GetValue(ev) + delta, facet))
  201.     {        
  202.         // Propagate the scrolling notification and ask to scroll
  203.         FW_Fixed fxDelta = FW_IntToFixed(delta);
  204.         ScrollPositionChanged(ev, fxDelta, true);
  205.     }
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // FW_CScrollBar::Direction
  210. //----------------------------------------------------------------------------------------
  211. //    Figure out which direction we're scrolling
  212.  
  213. FW_XYSelector FW_CScrollBar::ScrollingDirection(Environment *ev) const
  214. {
  215.     FW_CRect bounds = GetBounds(ev);
  216.     return (bounds.Height() > bounds.Width()) ? FW_kVertical : FW_kHorizontal;
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // FW_CScrollBar::ScrollPositionChanged
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CScrollBar::ScrollPositionChanged(Environment *ev, FW_Fixed delta, FW_Boolean shouldScroll)
  223. {
  224.     // ----- Notify receivers of the changes in scroll position
  225.     FW_CScrollNotification notification(this, ScrollingDirection(ev), delta, shouldScroll);
  226.     Notify(ev, notification);
  227. }
  228.  
  229. //----------------------------------------------------------------------------------------
  230. // FW_CScrollBar::GetDefaultScrollBarSize
  231. //----------------------------------------------------------------------------------------
  232.  
  233. FW_CPoint FW_CScrollBar::GetDefaultScrollBarSize()
  234. {
  235. #ifdef FW_BUILD_MAC
  236.     return FW_CPoint(FW_IntToFixed(15), FW_IntToFixed(15));
  237. #endif
  238. #ifdef FW_BUILD_WIN
  239.     return FW_CPoint(FW_IntToFixed(::GetSystemMetrics(SM_CXHSCROLL)),
  240.                      FW_IntToFixed(::GetSystemMetrics(SM_CXVSCROLL)));
  241. #endif
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. // FW_CScrollBar::DoActivateEvent
  246. //----------------------------------------------------------------------------------------
  247.  
  248. void FW_CScrollBar::DoActivateEvent(Environment *ev, const FW_CActivateEvent& theActivateEvent)
  249. {
  250.     if (IsVisible(ev) == false)
  251.         return;
  252.  
  253. #ifdef FW_BUILD_MAC
  254.     ODFacet* facet = theActivateEvent.GetFacet(ev);
  255.     FW_CViewContext vc(ev, this, facet);
  256.  
  257.     FW_CPlatformPoint qdPoint    = vc.LogicalToDevice(FW_kZeroPoint);
  258.     FW_CPlatformPoint qdSize    = GetSize(ev).AsPlatformPoint();
  259.  
  260.     if (theActivateEvent.IsActivating(ev))
  261.     {    
  262.         // Draw scrollbar immediately
  263.         fControlHelper->Show(qdPoint, qdSize);
  264.     }
  265.     else
  266.     {
  267.         fControlHelper->Hide();
  268.         Draw(ev, facet, NULL);
  269.     }
  270. #endif
  271.  
  272. #ifdef FW_BUILD_WIN
  273.     // [TODO]
  274. #endif
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // FW_CScrollBar::SetMinorScrollUnits
  279. //----------------------------------------------------------------------------------------
  280.  
  281. void FW_CScrollBar::SetMinorScrollUnits(Environment *ev, FW_Fixed units)
  282. {
  283. FW_UNUSED(ev);
  284.     fMinorScrollUnits = units;
  285. }
  286.  
  287. //----------------------------------------------------------------------------------------
  288. // FW_CScrollBar::SetMajorScrollUnits
  289. //----------------------------------------------------------------------------------------
  290.  
  291. void FW_CScrollBar::SetMajorScrollUnits(Environment *ev, FW_Fixed units)
  292. {
  293. FW_UNUSED(ev);
  294.     fMajorScrollUnits = units;
  295. }
  296.  
  297. //----------------------------------------------------------------------------------------
  298. // FW_CScrollBar::SetScrollMax
  299. //----------------------------------------------------------------------------------------
  300.  
  301. void FW_CScrollBar::SetScrollMax(Environment *ev, FW_Fixed units)
  302. {
  303. FW_UNUSED(ev);
  304. #ifdef FW_BUILD_MAC
  305.     fControlHelper->SetMax(FW_FixedToInt(units));
  306. #endif
  307.  
  308. #ifdef FW_BUILD_WIN
  309.     // [TODO]
  310.     int min, max;
  311.     fScrollBarHelper->GetScrollRange(min, max);
  312.     fScrollBarHelper->SetScrollRange(min, FW_FixedToInt(units));
  313. #endif
  314. }
  315.  
  316. //----------------------------------------------------------------------------------------
  317. // FW_CScrollBar::SetScrollMin
  318. //----------------------------------------------------------------------------------------
  319.  
  320. void FW_CScrollBar::SetScrollMin(Environment *ev, FW_Fixed units)
  321. {
  322. FW_UNUSED(ev);
  323. #ifdef FW_BUILD_MAC
  324.     fControlHelper->SetMin(FW_FixedToInt(units));
  325. #endif
  326.  
  327. #ifdef FW_BUILD_WIN
  328.     // [TODO]
  329.     int min, max;
  330.     fScrollBarHelper->GetScrollRange(min, max);
  331.     fScrollBarHelper->SetScrollRange(FW_FixedToInt(units), max);
  332. #endif
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. // FW_CScrollBar::SetScrollPos
  337. //----------------------------------------------------------------------------------------
  338.  
  339. void FW_CScrollBar::SetScrollPos(Environment *ev, FW_Fixed units)
  340. {
  341.     SetValue(ev, FW_FixedToInt(units));    
  342. }
  343.  
  344. //----------------------------------------------------------------------------------------
  345. // FW_CScrollBar::SetScrollPos
  346. //----------------------------------------------------------------------------------------
  347.  
  348. void FW_CScrollBar::SetScrollPos(Environment *ev, FW_Fixed range, FW_Fixed pos)
  349. {
  350.     FW_Fixed units = FW_kFixed0;
  351.     if (range != FW_kFixed0)
  352.         units = FW_WideMultiply(GetScrollMax(ev) - GetScrollMin(ev), pos) / range + GetScrollMin(ev);
  353.  
  354.     SetValue(ev, FW_FixedToInt(units));    
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. //    FW_CScrollBar::Flatten
  359. //----------------------------------------------------------------------------------------
  360.  
  361. void FW_CScrollBar::Flatten(Environment* ev, FW_CWritableStream& stream) const
  362. {
  363.     FW_CNativeControl::Flatten(ev, stream);
  364.  
  365.     short min = fControlHelper->GetMin();
  366.     short max = fControlHelper->GetMax();
  367.     
  368.     stream << min;
  369.     stream << max;
  370.     stream << fMinorScrollUnits;
  371.     stream << fMajorScrollUnits;
  372. }
  373.  
  374. //----------------------------------------------------------------------------------------
  375. //    FW_CScrollBar::InitializeFromStream
  376. //----------------------------------------------------------------------------------------
  377.  
  378. void FW_CScrollBar::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  379. {
  380.     FW_CNativeControl::InitializeFromStream(ev, stream);
  381.     
  382.     short min, max;
  383.     FW_Fixed major, minor;
  384.  
  385.     stream >> min >> max;
  386.     stream >> minor >> major;
  387.  
  388.     SetMinorScrollUnits(ev, minor);
  389.     SetMajorScrollUnits(ev, major);
  390.     
  391.     // [LSD] check for valid 16 bits value for scroll-bar
  392.     FW_ControlValue value = GetValue(ev);
  393.     FW_ASSERT(value <= FW_MAXINT16 && value >= FW_MININT16);
  394.  
  395.     Initialize(ev, value, min, max);
  396. }
  397.  
  398. //----------------------------------------------------------------------------------------
  399. // FW_CScrollBar::GetMinorScrollUnits
  400. //----------------------------------------------------------------------------------------
  401.  
  402. FW_Fixed FW_CScrollBar::GetMinorScrollUnits(Environment*) const
  403. {
  404.     return fMinorScrollUnits;
  405. }
  406.  
  407. //----------------------------------------------------------------------------------------
  408. // FW_CScrollBar::GetMajorScrollUnits
  409. //----------------------------------------------------------------------------------------
  410.  
  411. FW_Fixed FW_CScrollBar::GetMajorScrollUnits(Environment*) const
  412. {
  413.     return fMajorScrollUnits;
  414. }
  415.  
  416. //----------------------------------------------------------------------------------------
  417. // FW_CScrollBar::GetScrollMax
  418. //----------------------------------------------------------------------------------------
  419.  
  420. FW_Fixed FW_CScrollBar::GetScrollMax(Environment*) const
  421. {
  422. #ifdef FW_BUILD_MAC
  423.     return FW_IntToFixed(fControlHelper->GetMax());
  424. #endif
  425.  
  426. #ifdef FW_BUILD_WIN
  427.     int min, max;
  428.     fScrollBarHelper->GetScrollRange(min, max);
  429.     return FW_IntToFixed(max);
  430. #endif
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. // FW_CScrollBar::GetScrollMin
  435. //----------------------------------------------------------------------------------------
  436.  
  437. FW_Fixed FW_CScrollBar::GetScrollMin(Environment*) const
  438. {
  439. #ifdef FW_BUILD_MAC
  440.     return FW_IntToFixed(fControlHelper->GetMin());
  441. #endif
  442.  
  443. #ifdef FW_BUILD_WIN
  444.     int min, max;
  445.     fScrollBarHelper->GetScrollRange(min, max);
  446.     return FW_IntToFixed(min);
  447. #endif
  448. }
  449.  
  450. //----------------------------------------------------------------------------------------
  451. // FW_CScrollBar::GetScrollPos
  452. //----------------------------------------------------------------------------------------
  453.  
  454. FW_Fixed FW_CScrollBar::GetScrollPos(Environment*) const
  455. {
  456. #ifdef FW_BUILD_MAC
  457.     return FW_IntToFixed(fControlHelper->GetValue());
  458. #endif
  459.  
  460. #ifdef FW_BUILD_WIN
  461.     return FW_IntToFixed(fScrollBarHelper->GetScrollPos());
  462. #endif
  463. }
  464.  
  465. //----------------------------------------------------------------------------------------
  466. // FW_CScrollBar::PrivAttachToScroller
  467. //----------------------------------------------------------------------------------------
  468.  
  469. void FW_CScrollBar::PrivAttachToScroller(Environment*ev, FW_CPrivBaseScroller* scroller)
  470. {
  471. FW_UNUSED(ev);
  472.     fScroller = scroller;
  473. }